﻿using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;

namespace MVCForum.Web.Identity.EF {

	public class EntityStore<TEntity> where TEntity : class {
		private DbContext context;

		private IDbSet<TEntity> DbEntitySet { get; set; }

		internal IQueryable<TEntity> EntitySet {
			get {
				return this.DbEntitySet;
			}
		}

		public EntityStore(DbContext context) {
			if (context == null) {
				throw new ArgumentNullException("context");
			}
			this.context = context;
			this.DbEntitySet = context.Set<TEntity>();
		}

		public void Create(TEntity entity) {
			this.DbEntitySet.Add(entity);
		}

		public void Delete(TEntity entity) {
			this.context.Entry<TEntity>(entity).State = EntityState.Deleted;
		}

		public Task<TEntity> GetByIdAsync(Object id) {
			return Task.FromResult<TEntity>(this.DbEntitySet.Find(new Object[] { id }));
		}
	}
}